<?php
namespace app\admin\controller;
use think\Db;
class Tuiguang extends Common
{
//  渠道列表页面
    public function index()
    {
		$qudao=$this->qudao();//调用渠道数据
		$this->assign('qudao',$qudao);
		return $this->fetch();	
    }
	
//	 渠道类表数据
    public function get_list()
    {
		$TgadminID= input('TgadminID');
	    $where=[];
		if($TgadminID){
			$where['ID'] = $TgadminID;
		}
		$page = input('page');
		$limit = input('limit');
		$songsong = input('songsong');
		$re = db('tg_admin')->alias('a')
            ->join('xb_sys_administrator s','a.AdminID = s.ID','LEFT')
            ->field('a.*,s.UserName')
            ->where($where)
            ->group('a.ID')
            ->page($page,$limit)
            ->select();
		foreach ($re as $k => $v) {
			if($v['Status']=='0'){
				$re[$k]['Status']='<span style="color:red;">禁用</span>';
			}
			if($v['Status']=='1'){
				$re[$k]['Status']='<span style="color:green;">启用</span>';
			}
			$re[$k]['Links']="http://".$_SERVER['HTTP_HOST']."/index.php/Register/tuiguang?puser=".$v['Name'];
		}
		foreach ($re as $k => $v) {
			if($v['KouliangClick']=='0'){
				$re[$k]['KouliangClick']='<span style="color:red;">禁用</span>';
			}
			if($v['KouliangClick']=='1'){
				$re[$k]['KouliangClick']='<span style="color:green;">启用</span>';
			}
			$re[$k]['Links']="http://".$_SERVER['HTTP_HOST']."/admin.php/login/register?puser=".$v['Name'];
		}
		$cuont=Db::table('xb_tg_admin')->where($where)->count('ID');
		if($re){
		   songsong($re,$cuont);
		}
    }
	

//	编辑页面
		public function editss(){
		$ID= input('ID');
		$data=Db::table('xb_tg_admin')->where('ID',$ID)->find();

        //获取所有上架中的商品
        $goodsAll = model('Goods')->getAllGoods('ID,SalePrice');
        $this->assign('goodsAll',$goodsAll);

//		p($data);die;
		$this->assign('data',$data);
		return $this->fetch();
	}
	//	编辑操作
	public function edit(){
	    $this->logs('渠道编辑');
		$data= input();
		$data['UpdateTime']=date('Y-m-d H:i:s');//时间

        if(input('join') !== 'join'){
            if(!empty($data['Status']) && $data['Status']=='on'){
                $data['Status']='1';
            }else{
                $data['Status']='0';
            }
            if(!empty($data['KouliangClick']) && $data['KouliangClick']=='on'){
                $data['KouliangClick']='1';
            }else{
                $data['KouliangClick']='0';
            };
        }else{
            unset($data['join']);
        }

		$re=Db::table('xb_tg_admin')->update($data);
		if($re){
	        show_success();
		}else{
			show_error();
		}
	}
		//	更改 渠道状态
//	public function edit_status(){
//		
//		$data=input();
//		$data['UpdateTime']=date('Y-m-d H:i:s');//时间
//		if($data['Status']=='true'){
//			$data['Status']='1';
//		}else{
//			$data['Status']='0';
//		}
//		$re=Db::table('xb_tg_admin')->update($data);
//		if($re){
//	    return TRUE;
//		}else{
//			return FALSE;
//		}	
//	}
	
	
//	添加  渠道页面
	public function addss(){
		$data=Db::table('xb_tg_admin')->select();

		//获取所有上架中的商品
        $goodsAll = model('Goods')->getAllGoods('ID,SalePrice');
        $this->assign('goodsAll',$goodsAll);

		$this->assign('data',$data);
		return $this->fetch();	
	}

    /**
     * 添加 渠道操作
     */
	public function add()
    {
		$this->logs('添加渠道');

		$postData = $postData = Request()->only('Name,DaichaoName,Kouliang,TuiGuangFangShi,DanJia,TgFeiYong,jishenfen,rengongfen,TemplateField,GoodsID');
        $postData['CreateTime'] = date('Y-m-d H:i:s');//审核时间
        $result = db('tg_admin')->insert($postData);
        if($result){
            show_success();
        }else{
            show_error('','添加失败');
        }
	}

	/**
     * 渠道关联贷超角色用户
     */
	public function join()
    {
        //获取所有贷超角色
        $data = db('sys_administrator')->where(['RoleID' => 48])->select();
        $this->assign('data',$data);
        $this->assign('id',input('ID'));
        return view();
    }
}
